{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22f48348-7464-4b00-a99e-e109396b0ed1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 推荐系统样例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "022c5e58-07bd-4c92-b4a3-2d7f61be1426",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 0/1型数据样例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3e93df9a-9a90-46ec-815b-b870cb12d8ae",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "users = [\"User1\",\"User2\",\"User3\",\"User4\",\"User5\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8cbbd5e7-aac5-437b-9ec2-fe38d55321a2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "items = [\"Item A\",\"Item B\",\"Item C\",\"Item D\",\"Item E\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "22176fe1-216a-4703-8d58-6c7d41980c02",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 用户购买记录数据集\n",
    "datasets = [\n",
    "    [1,0,1,1,0],\n",
    "    [1,0,0,1,1],\n",
    "    [1,0,1,0,0],\n",
    "    [0,1,0,1,1],\n",
    "    [1,1,1,0,1]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bcc8bd72-6307-4af1-8b1b-1290f21ba877",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8b8422c-6c3a-4dda-9537-1c2a503b5b8b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(datasets,columns=items,index=users)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ec1c1d0-fbdd-403c-9558-2d1d69229015",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Item A</th>\n",
       "      <th>Item B</th>\n",
       "      <th>Item C</th>\n",
       "      <th>Item D</th>\n",
       "      <th>Item E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>User1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>User5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Item A  Item B  Item C  Item D  Item E\n",
       "User1       1       0       1       1       0\n",
       "User2       1       0       0       1       1\n",
       "User3       1       0       1       0       0\n",
       "User4       0       1       0       1       1\n",
       "User5       1       1       1       0       1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e4d873e-af05-4aff-b99c-d072657d784d",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>User1</th>\n",
       "      <th>User2</th>\n",
       "      <th>User3</th>\n",
       "      <th>User4</th>\n",
       "      <th>User5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Item A</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Item B</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Item C</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Item D</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Item E</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        User1  User2  User3  User4  User5\n",
       "Item A      1      1      1      0      1\n",
       "Item B      0      0      0      1      1\n",
       "Item C      1      0      1      0      1\n",
       "Item D      1      1      0      1      0\n",
       "Item E      0      1      0      1      1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "91c3560a-964f-4c6b-a52a-0f0ec63f35b2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics import jaccard_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c62a43b8-14e9-41e9-b4e2-cb45e21b2762",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jaccard_score(df[\"Item A\"],df[\"Item B\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2504db85-0b7c-4bfd-bf41-bd6c26f0cf94",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.metrics.pairwise import pairwise_distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0beeac6a-da5d-437d-b56a-4f0de9cf733a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\install\\python37\\lib\\site-packages\\sklearn\\metrics\\pairwise.py:1870: DataConversionWarning: Data was converted to boolean for metric jaccard\n",
      "  warnings.warn(msg, DataConversionWarning)\n"
     ]
    }
   ],
   "source": [
    "user_similar = 1 - pairwise_distances(df.values, metric=\"jaccard\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "246a5056-8e73-4cc5-bffe-beef5167adf5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户之间的两两相似度：\n",
      "          User1  User2     User3  User4  User5\n",
      "User1  1.000000   0.50  0.666667    0.2    0.4\n",
      "User2  0.500000   1.00  0.250000    0.5    0.4\n",
      "User3  0.666667   0.25  1.000000    0.0    0.5\n",
      "User4  0.200000   0.50  0.000000    1.0    0.4\n",
      "User5  0.400000   0.40  0.500000    0.4    1.0\n"
     ]
    }
   ],
   "source": [
    "user_similar = pd.DataFrame(user_similar, columns=users, index=users)\n",
    "print(\"用户之间的两两相似度：\")\n",
    "print(user_similar)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72efccce-9b6a-4b0c-bdce-4befdbf8121c",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 基于用户的 协同过滤-User-Based CF  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c4d79c7-8dcb-47eb-9901-bc476797bc33",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**因为df的每一行 是一条完整的数据，所以计算每一行数据之间的 两两相似度**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f1aeb314-10f1-44aa-b9de-3bd978fcefb5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top2相似用户：\n",
      "{'User1': ['User3', 'User2'], 'User2': ['User1', 'User4'], 'User3': ['User1', 'User5'], 'User4': ['User2', 'User5'], 'User5': ['User3', 'User1']}\n"
     ]
    }
   ],
   "source": [
    "topN_users = {}\n",
    "# 遍历每一行数据\n",
    "for i in user_similar.index:\n",
    "    # 取出每一列数据，并删除自身，然后排序数据\n",
    "    _df = user_similar.loc[i].drop([i])\n",
    "    _df_sorted = _df.sort_values(ascending=False)\n",
    "\n",
    "    top2 = list(_df_sorted.index[:2])\n",
    "    topN_users[i] = top2\n",
    "\n",
    "print(\"Top2相似用户：\")\n",
    "print(topN_users)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "95d3a7d3-3378-4ad9-8fa3-b99815e5da62",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终推荐结果：\n",
      "{'User1': {'Item E'}, 'User2': {'Item B', 'Item C'}, 'User3': {'Item E', 'Item B', 'Item D'}, 'User4': {'Item A', 'Item C'}, 'User5': {'Item D'}}\n"
     ]
    }
   ],
   "source": [
    "rs_results = {}\n",
    "# 构建推荐结果\n",
    "for user, sim_users in topN_users.items():\n",
    "    rs_result = set()    # 存储推荐结果\n",
    "    for sim_user in sim_users:\n",
    "        # 构建初始的推荐结果\n",
    "        rs_result = rs_result.union(set(df.loc[sim_user].replace(0,np.nan).dropna().index))\n",
    "    # 过滤掉已经购买过的物品\n",
    "    # print(rs_result) #  相似用户 购买的物品\n",
    "    # print(set(df.loc[user].replace(0,np.nan).dropna().index))  # 用户 够买的物品\n",
    "    rs_result -= set(df.loc[user].replace(0,np.nan).dropna().index)\n",
    "    # print(rs_result) # 去掉 自己够买的物品  = 推荐的物品\n",
    "    # assert 0\n",
    "    rs_results[user] = rs_result\n",
    "print(\"最终推荐结果：\")\n",
    "print(rs_results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef3b976e-2d38-4e60-a0b4-364282f46434",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 基于 物品的 协同过滤-Item-Based CF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e7e90bfb-ef85-41c8-94df-0fea1af245c0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "物品之间的两两相似度：\n",
      "        Item A    Item B  Item C  Item D    Item E\n",
      "Item A    1.00  0.200000    0.75    0.40  0.400000\n",
      "Item B    0.20  1.000000    0.25    0.25  0.666667\n",
      "Item C    0.75  0.250000    1.00    0.20  0.200000\n",
      "Item D    0.40  0.250000    0.20    1.00  0.500000\n",
      "Item E    0.40  0.666667    0.20    0.50  1.000000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\install\\python37\\lib\\site-packages\\sklearn\\metrics\\pairwise.py:1870: DataConversionWarning: Data was converted to boolean for metric jaccard\n",
      "  warnings.warn(msg, DataConversionWarning)\n"
     ]
    }
   ],
   "source": [
    "# 计算物品间相似度\n",
    "item_similar = 1 - pairwise_distances(df.T.values, metric=\"jaccard\")\n",
    "item_similar = pd.DataFrame(item_similar, columns=items, index=items)\n",
    "print(\"物品之间的两两相似度：\")\n",
    "print(item_similar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "827f8780-6435-4be8-af6c-46f032e693bb",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top2相似物品：\n",
      "{'Item A': ['Item C', 'Item D'], 'Item B': ['Item E', 'Item C'], 'Item C': ['Item A', 'Item B'], 'Item D': ['Item E', 'Item A'], 'Item E': ['Item B', 'Item D']}\n"
     ]
    }
   ],
   "source": [
    "topN_items = {}\n",
    "# 遍历每一行数据\n",
    "for i in item_similar.index:\n",
    "    # 取出每一列数据，并删除自身，然后排序数据\n",
    "    _df = item_similar.loc[i].drop([i])\n",
    "    _df_sorted = _df.sort_values(ascending=False)\n",
    "\n",
    "    top2 = list(_df_sorted.index[:2])\n",
    "    topN_items[i] = top2\n",
    "print(\"Top2相似物品：\")\n",
    "print(topN_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40aec58d-69a3-4ad9-8d09-274badd6f832",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User1\n",
      "{'Item E', 'Item B', 'Item A', 'Item D', 'Item C'}\n",
      "{'Item A', 'Item D', 'Item C'}\n",
      "{'Item E', 'Item B'}\n",
      "**********\n",
      "User2\n",
      "{'Item E', 'Item B', 'Item A', 'Item D', 'Item C'}\n",
      "{'Item E', 'Item A', 'Item D'}\n",
      "{'Item B', 'Item C'}\n",
      "**********\n",
      "User3\n",
      "{'Item B', 'Item A', 'Item D', 'Item C'}\n",
      "{'Item A', 'Item C'}\n",
      "{'Item B', 'Item D'}\n",
      "**********\n",
      "User4\n",
      "{'Item E', 'Item B', 'Item A', 'Item D', 'Item C'}\n",
      "{'Item B', 'Item E', 'Item D'}\n",
      "{'Item A', 'Item C'}\n",
      "**********\n",
      "User5\n",
      "{'Item E', 'Item B', 'Item A', 'Item D', 'Item C'}\n",
      "{'Item B', 'Item E', 'Item A', 'Item C'}\n",
      "{'Item D'}\n",
      "**********\n",
      "最终推荐结果：\n",
      "{'User1': {'Item E', 'Item B'}, 'User2': {'Item B', 'Item C'}, 'User3': {'Item B', 'Item D'}, 'User4': {'Item A', 'Item C'}, 'User5': {'Item D'}}\n"
     ]
    }
   ],
   "source": [
    "rs_results = {}\n",
    "# 构建推荐结果\n",
    "for user in df.index:    # 遍历所有用户\n",
    "    rs_result = set()\n",
    "    for item in df.loc[user].replace(0,np.nan).dropna().index:   # 取出每个用户当前已购物品列表\n",
    "        # 根据每个物品找出最相似的TOP-N物品，构建初始推荐结果\n",
    "        rs_result = rs_result.union(topN_items[item])  # 找到与物品A 相似 的相关物品\n",
    "    print(user)\n",
    "    print(rs_result)\n",
    "    print(set(df.loc[user].replace(0,np.nan).dropna().index))\n",
    "    # 过滤掉用户已购的物品\n",
    "    rs_result -= set(df.loc[user].replace(0,np.nan).dropna().index)\n",
    "    print(rs_result)\n",
    "    print(\"*\"*10)\n",
    "    # assert 0\n",
    "    # 添加到结果中\n",
    "    rs_results[user] = rs_result\n",
    "\n",
    "print(\"最终推荐结果：\")\n",
    "print(rs_results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b4899df-71f2-416d-870d-fa094b99ccd9",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 评分型/连续型数据样例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d3ab55c-d01f-40e6-aef2-1aa20b337cf1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**使用协同过滤推荐算法对用户进行评分预测** \n",
    "</br>\n",
    "* 构建数据集：对于缺失的部分我们需要保留为None，如果设置为0那么会被当作评分值为0去对待"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "928994e2-2914-4a51-936d-78695e7b951f",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "users = [\"User1\", \"User2\", \"User3\", \"User4\", \"User5\"]\n",
    "items = [\"Item A\", \"Item B\", \"Item C\", \"Item D\", \"Item E\"]\n",
    "# 用户购买记录数据集\n",
    "datasets = [\n",
    "    [5,3,4,4,None],\n",
    "    [3,1,2,3,3],\n",
    "    [4,3,4,3,5],\n",
    "    [3,3,1,5,4],\n",
    "    [1,5,5,2,1],\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8cb518c-7b95-4e28-93af-66dd4c46983f",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**计算相似度：对于评分数据这里我们采用皮尔逊相关系数[-1,1]来计算，-1表示强负相关，+1表示强正相关**\n",
    "</br>\n",
    "* pandas中corr方法可直接用于计算皮尔逊相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "153eb2df-c7a4-4e1e-bc92-67dd7d05536b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Item A  Item B  Item C  Item D  Item E\n",
      "User1       5       3       4       4     NaN\n",
      "User2       3       1       2       3     3.0\n",
      "User3       4       3       4       3     5.0\n",
      "User4       3       3       1       5     4.0\n",
      "User5       1       5       5       2     1.0\n",
      "用户之间的两两相似度：\n",
      "        User1   User2   User3   User4   User5\n",
      "User1  1.0000  0.8528  0.7071  0.0000 -0.7921\n",
      "User2  0.8528  1.0000  0.4677  0.4900 -0.9001\n",
      "User3  0.7071  0.4677  1.0000 -0.1612 -0.4666\n",
      "User4  0.0000  0.4900 -0.1612  1.0000 -0.6415\n",
      "User5 -0.7921 -0.9001 -0.4666 -0.6415  1.0000\n",
      "物品之间的两两相似度：\n",
      "        Item A  Item B  Item C  Item D  Item E\n",
      "Item A  1.0000 -0.4767 -0.1231  0.5322  0.9695\n",
      "Item B -0.4767  1.0000  0.6455 -0.3101 -0.4781\n",
      "Item C -0.1231  0.6455  1.0000 -0.7206 -0.4276\n",
      "Item D  0.5322 -0.3101 -0.7206  1.0000  0.5817\n",
      "Item E  0.9695 -0.4781 -0.4276  0.5817  1.0000\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame(datasets,\n",
    "                  columns=items,\n",
    "                  index=users)\n",
    "print(df)\n",
    "print(\"用户之间的两两相似度：\")\n",
    "# 直接计算皮尔逊相关系数\n",
    "# 默认是按列进行计算，因此如果计算用户间的相似度，当前需要进行转置\n",
    "user_similar = df.T.corr()\n",
    "print(user_similar.round(4))\n",
    "\n",
    "print(\"物品之间的两两相似度：\")\n",
    "item_similar = df.corr()\n",
    "print(item_similar.round(4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "941f11b2-54df-4c5d-acb6-6ce424d8abcb",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "可以看到与用户1最相似的是用户2和用户3；与物品A最相似的物品分别是物品E和物品D。\n",
    "\n",
    "**注意:** 我们在预测评分时，往往是通过与其有正相关的用户或物品进行预测，如果不存在正相关的情况，那么将无法做出预测。这一点尤其是在稀疏评分矩阵中尤为常见，因为稀疏评分矩阵中很难得出正相关系数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29132ef0-0f0a-4b6e-b713-d4984438079c",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "**评分预测:**\n",
    "User-Based CF 评分预测：使用用户间的相似度进行预测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8441536a-c2c6-4328-9ff6-3e5f75c32975",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "关于评分预测的方法也有比较多的方案，下面介绍一种效果比较好的方案，该方案考虑了用户本身的评分以及近邻用户的加权平均相似度打分来进行预测："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d34257a-9529-4477-b0cb-81b9f1e74c38",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "url: https://xfliu1998.github.io/2022/01/18/5.1-Recommendation-System-Introduction/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1b09b36-d6b0-4118-a16c-5e400d815ee4",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Item-Based CF 评分预测：使用物品间的相似度进行预测\n",
    "\n",
    "这里利用物品相似度预测的计算同上，同样考虑了用户自身的平均打分因素，结合预测物品与相似物品的加权平均相似度打分进行来进行预测"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0195bd1-a1e7-47eb-9d00-8c2f22f5795d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "url: https://xfliu1998.github.io/2022/01/18/5.1-Recommendation-System-Introduction/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dfd3cf3-a4c4-499e-a60c-773b079a58e6",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}